//
//  UFViewUtil.m
//  UniversalFramework
//
//  Created by 谢朋 on 2016/11/2.
//  Copyright © 2017年 北京华星成汇文化发展有限公司. All rights reserved.
//

#import "UFViewUtil.h"

@implementation UFViewUtil

#pragma mark - 颜色
+ (UIColor *)colorByRgbHex:(NSString *)rgbHex {
    return [self colorByRgbHex:rgbHex alpha:1];
}

+ (UIColor *)colorByRgbHex:(NSString *)rgbHex alpha:(float)alpha{
    if (rgbHex == nil) {
        return nil;
    }
    
    unsigned int hexInt = 0;
    NSScanner *scanner = [NSScanner scannerWithString:rgbHex];
    [scanner setCharactersToBeSkipped:[NSCharacterSet characterSetWithCharactersInString:@"#"]];
    [scanner scanHexInt:&hexInt];
    return [UIColor colorWithRed:((float) ((hexInt & 0xFF0000) >> 16)) / 255.0 green:((float)((hexInt & 0xFF00) >> 8)) / 255.0 blue:((float)(hexInt & 0xFF)) / 255.0 alpha:alpha];
}

#pragma mark - 屏幕方向
+ (UFScreenOrientation)screenOrientation:(UIInterfaceOrientation)interfaceOrientation {
    return (interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) ? UFScreenOrientationPortrait : UFScreenOrientationLandscape;
}

+ (CGSize)screenSize:(UFScreenOrientation)screenOrientation {
    CGSize screenSize = [UIScreen mainScreen].bounds.size;
    return screenOrientation == UFScreenOrientationPortrait ? screenSize : CGSizeMake(screenSize.height, screenSize.width);
}

+ (UFScreenOrientation)screenOrientationByWidth:(CGFloat)screenWidth {
    return [self screenSize:UFScreenOrientationPortrait].width == screenWidth ? UFScreenOrientationPortrait : UFScreenOrientationLandscape;
}

#pragma mark - 布局
+ (void)addConstraintWithItem:(UIView *)view1 toItem:(UIView *)view2 relativeTop:(CGFloat)top {
    view1.translatesAutoresizingMaskIntoConstraints = NO;
    [view1.superview addConstraint:[NSLayoutConstraint constraintWithItem:view1 attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:view2 attribute:NSLayoutAttributeTop multiplier:1 constant:top]];
}

+ (void)addConstraintWithItem:(UIView *)view1 toItem:(UIView *)view2 relativeBottom:(CGFloat)bottom {
    view1.translatesAutoresizingMaskIntoConstraints = NO;
    [view1.superview addConstraint:[NSLayoutConstraint constraintWithItem:view1 attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:view2 attribute:NSLayoutAttributeBottom multiplier:1 constant:bottom]];
}

+ (void)addConstraintWithItem:(UIView *)view1 toItem:(UIView *)view2 relativeLeft:(CGFloat)left {
    view1.translatesAutoresizingMaskIntoConstraints = NO;
    [view1.superview addConstraint:[NSLayoutConstraint constraintWithItem:view1 attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:view2 attribute:NSLayoutAttributeLeft multiplier:1 constant:left]];
}

+ (void)addConstraintWithItem:(UIView *)view1 toItem:(UIView *)view2 relativeRight:(CGFloat)right {
    view1.translatesAutoresizingMaskIntoConstraints = NO;
    [view1.superview addConstraint:[NSLayoutConstraint constraintWithItem:view1 attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:view2 attribute:NSLayoutAttributeRight multiplier:1 constant:right]];
}

+ (void)addConstraintWithItem:(UIView *)view1 toItem:(UIView *)view2 relativeCenterX:(CGFloat)centerX {
    view1.translatesAutoresizingMaskIntoConstraints = NO;
    [view1.superview addConstraint:[NSLayoutConstraint constraintWithItem:view1 attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:view2 attribute:NSLayoutAttributeCenterX multiplier:1 constant:centerX]];
}

+ (void)addConstraintWithItem:(UIView *)view1 toItem:(UIView *)view2 relativeCenterY:(CGFloat)centerY {
    view1.translatesAutoresizingMaskIntoConstraints = NO;
    [view1.superview addConstraint:[NSLayoutConstraint constraintWithItem:view1 attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:view2 attribute:NSLayoutAttributeCenterY multiplier:1 constant:centerY]];
}

+ (void)addConstraintWithItem:(UIView *)view1 toItem:(UIView *)view2 relativeTopToBottom:(CGFloat)topToBottom {
    view1.translatesAutoresizingMaskIntoConstraints = NO;
    [view1.superview addConstraint:[NSLayoutConstraint constraintWithItem:view1 attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:view2 attribute:NSLayoutAttributeBottom multiplier:1 constant:topToBottom]];
}

+ (void)addConstraintWithItem:(UIView *)view1 toItem:(UIView *)view2 relativeBottomToTop:(CGFloat)bottomToTop {
    view1.translatesAutoresizingMaskIntoConstraints = NO;
    [view1.superview addConstraint:[NSLayoutConstraint constraintWithItem:view1 attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:view2 attribute:NSLayoutAttributeTop multiplier:1 constant:bottomToTop]];
}

+ (void)addConstraintWithItem:(UIView *)view1 toItem:(UIView *)view2 relativeLeftToRight:(CGFloat)leftToRight {
    view1.translatesAutoresizingMaskIntoConstraints = NO;
    [view1.superview addConstraint:[NSLayoutConstraint constraintWithItem:view1 attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:view2 attribute:NSLayoutAttributeRight multiplier:1 constant:leftToRight]];
}

+ (void)addConstraintWithItem:(UIView *)view1 toItem:(UIView *)view2 relativeRightToLeft:(CGFloat)rightToLeft {
    view1.translatesAutoresizingMaskIntoConstraints = NO;
    [view1.superview addConstraint:[NSLayoutConstraint constraintWithItem:view1 attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:view2 attribute:NSLayoutAttributeLeft multiplier:1 constant:rightToLeft]];
}

+ (void)addConstraintWithItem:(UIView *)view relativeTop:(CGFloat)top relativeLeft:(CGFloat)left {
    [self addConstraintWithItem:view toItem:view.superview relativeTop:top];
    [self addConstraintWithItem:view toItem:view.superview relativeLeft:left];
}

+ (void)addConstraintWithItem:(UIView *)view relativeBottom:(CGFloat)bottom relativeLeft:(CGFloat)left {
    [self addConstraintWithItem:view toItem:view.superview relativeBottom:bottom];
    [self addConstraintWithItem:view toItem:view.superview relativeLeft:left];
}

+ (void)addConstraintWithItem:(UIView *)view relativeTop:(CGFloat)top relativeRight:(CGFloat)right {
    [self addConstraintWithItem:view toItem:view.superview relativeTop:top];
    [self addConstraintWithItem:view toItem:view.superview relativeRight:right];
}

+ (void)addConstraintWithItem:(UIView *)view relativeBottom:(CGFloat)bottom relativeRight:(CGFloat)right {
    [self addConstraintWithItem:view toItem:view.superview relativeBottom:bottom];
    [self addConstraintWithItem:view toItem:view.superview relativeRight:right];
}

+ (void)addConstraintWithItem:(UIView *)view relativeCenterX:(CGFloat)centerX relativeCenterY:(CGFloat)centerY {
    [self addConstraintWithItem:view toItem:view.superview relativeCenterX:centerX];
    [self addConstraintWithItem:view toItem:view.superview relativeCenterY:centerY];
}

+ (void)addConstraintWithItem:(UIView *)view1 toItem:(UIView *)view2 relativeWidth:(CGFloat)width {
    view1.translatesAutoresizingMaskIntoConstraints = NO;
    [view1.superview addConstraint:[NSLayoutConstraint constraintWithItem:view1 attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:view2 attribute:NSLayoutAttributeWidth multiplier:1 constant:width]];
}

+ (void)addConstraintWithItem:(UIView *)view1 toItem:(UIView *)view2 relativeHeight:(CGFloat)height {
    view1.translatesAutoresizingMaskIntoConstraints = NO;
    [view1.superview addConstraint:[NSLayoutConstraint constraintWithItem:view1 attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:view2 attribute:NSLayoutAttributeHeight multiplier:1 constant:height]];
}

+ (void)addConstraintWithItem:(UIView *)view1 absoluteWidth:(CGFloat)width {
    view1.translatesAutoresizingMaskIntoConstraints = NO;
    [view1 addConstraint:[NSLayoutConstraint constraintWithItem:view1 attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:0 constant:width]];
}

+ (void)addConstraintWithItem:(UIView *)view1 absoluteHeight:(CGFloat)height {
    view1.translatesAutoresizingMaskIntoConstraints = NO;
    [view1 addConstraint:[NSLayoutConstraint constraintWithItem:view1 attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:0 constant:height]];
}

+ (void)addConstraintWithItem:(UIView *)view relativeWidth:(CGFloat)width relativeHeight:(CGFloat)height {
    [self addConstraintWithItem:view toItem:view.superview relativeWidth:width];
    [self addConstraintWithItem:view toItem:view.superview relativeHeight:height];
}

+ (void)addConstraintWithItem:(UIView *)view absoluteWidth:(CGFloat)width relativeHeight:(CGFloat)height {
    [self addConstraintWithItem:view absoluteWidth:width];
    [self addConstraintWithItem:view toItem:view.superview relativeHeight:height];
}

+ (void)addConstraintWithItem:(UIView *)view relativeWidth:(CGFloat)width absoluteHeight:(CGFloat)height {
    [self addConstraintWithItem:view toItem:view.superview relativeWidth:width];
    [self addConstraintWithItem:view absoluteHeight:height];
}

+ (void)addConstraintWithItem:(UIView *)view absoluteWidth:(CGFloat)width absoluteHeight:(CGFloat)height {
    [self addConstraintWithItem:view absoluteWidth:width];
    [self addConstraintWithItem:view absoluteHeight:height];
}

+ (void)modifyConstraintWithItem:(UIView *)view1 absoluteWidth:(CGFloat)width {
    for (NSLayoutConstraint *constraint in view1.constraints) {
        if (constraint.firstAttribute == NSLayoutAttributeWidth && constraint.secondItem == nil) {
            constraint.constant = width;
            break;
        }
    }
}

+ (void)modifyConstraintWithItem:(UIView *)view1 absoluteHeight:(CGFloat)height {
    for (NSLayoutConstraint *constraint in view1.constraints) {
        if (constraint.firstAttribute == NSLayoutAttributeHeight && constraint.secondItem == nil) {
            constraint.constant = height;
            break;
        }
    }
}

+ (void)removeAllContraintWithItem:(UIView *)view {
    for (NSLayoutConstraint *constraint in view.superview.constraints) {
        if (constraint.firstItem == view) {
            [view.superview removeConstraint:constraint];
        }
    }
    for (NSLayoutConstraint *constraint in view.constraints) {
        if (constraint.firstItem == view) {
            [view removeConstraint:constraint];
        }
    }
}

#pragma mark - 其它
+ (void)showShortToast:(NSString *)message {
    UILabel *label = [[UILabel alloc] init];
    dispatch_async(dispatch_get_main_queue(), ^{
        label.font = [UIFont systemFontOfSize:14];
        label.textColor = [UIColor whiteColor];
        label.backgroundColor = [UIColor blackColor];
        label.layer.cornerRadius = 11;
        label.clipsToBounds = YES;
        label.textAlignment = NSTextAlignmentCenter;
        label.text = message;

        CGSize size = [message boundingRectWithSize:CGSizeMake(0, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{ NSFontAttributeName: label.font } context:nil].size;
        label.frame = CGRectMake(0, 0, size.width + 22, size.height + 22);
        UIWindow *window = [UIApplication sharedApplication].keyWindow;
        label.center = CGPointMake(window.center.x, window.bounds.size.height - 100);
        [[UIApplication sharedApplication].windows.lastObject addSubview:label];
    });
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t) (2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [label removeFromSuperview];
    });
}

+ (UFExpression *)expressionWithResult:(BOOL)result warning:(NSString *)warning {
    return [[UFExpression alloc] initWithResult:result warning:warning];
}

+ (BOOL)validateExpressions:(NSArray<UFExpression *> *)expressions {
    for (UFExpression *expression in expressions) {
        if (expression.result == YES) {
            [self showShortToast:expression.warning];
            return NO;
        }
    }
    return YES;
}

@end

@implementation UFExpression

- (instancetype)initWithResult:(BOOL)result warning:(NSString *)warning {
    self = [super init];
    if (self) {
        self->_result = result;
        self->_warning = warning;
    }
    return self;
}

@end
